package com.yaorange.controller;

import com.github.pagehelper.PageInfo;
import com.yaorange.api.vo.Result;
import com.yaorange.entity.Apply;
import com.yaorange.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: CorJ
 * @version: 1.0
 * @create: 2021-12-01 14:45
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;
    @GetMapping("/page")
    public Result<?> getPage(@RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "3")Integer pageSize,
                              String articleName,String applyDate,String applyName)
    {
       PageInfo<Apply> pageInfo =  applyService.getPageLst(pageNum,pageSize,articleName,applyDate,applyName);

        if(pageInfo!=null){
            return Result.ok(pageInfo);
        }
        return Result.error("error");
    }
    @PutMapping("/{applyId}/{stateId}/{articleId}/{applyNum}")
    public Result<?> update(@PathVariable Integer applyId,@PathVariable Integer stateId,@PathVariable Integer articleId,@PathVariable Integer applyNum){
        if(applyService.updateById(applyId,stateId,articleId,applyNum)){
            return Result.ok();
        }
        return Result.error("err");
    } @PutMapping("/pass")
    public Result<?> pass(@RequestParam Integer applyId,@RequestParam Integer stateId,@RequestParam Integer articleId,@RequestParam Integer applyNum){
        if(applyService.updateById(applyId,stateId,articleId,applyNum)){
            return Result.ok();
        }
        return Result.error("err");
    }
    @PutMapping("/{stateId}")
    public Result<?> updateMore(@RequestBody List<Integer> ids, @PathVariable Integer stateId){
        if(applyService.updateMoreById(ids,stateId)){
            return Result.ok();
        }
        return Result.error("err");
    }
    @PostMapping
    public Result<?> addApply(Integer userId,Integer articleId,Integer applyNum,String applyUnit,String applyReason,String applyDate,Integer id){
        Apply apply = new Apply();
        apply.setApplyDate(applyDate.substring(0,10));
        apply.setApplyNum(applyNum);
        apply.setApplyReason(applyReason);
        apply.setApplyUnit(applyUnit);
       apply.setUserId(userId);
       apply.setArticleId(articleId);
        apply.setState(0);
        if(applyService.addApply(apply,id)){
            return Result.ok("success");
        }
        return Result.error("err");
    }
    @GetMapping("/{id}")
    public Result<?> getStateList(@PathVariable Integer id){
       List<Apply> applyList = applyService.getStateList(id);
       return Result.ok(applyList);
    }
    @GetMapping
    public Result<?> applyList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize,
     Integer stateId,String applyDate, Integer userId){
        if(applyDate==null||applyDate.equals("")){
            String applyTime = "";
            PageInfo<Apply> pageInfo = applyService.getList(pageNum,pageSize,stateId,applyTime,userId);
            return Result.ok(pageInfo);
        }else {
            PageInfo<Apply> pageInfo = applyService.getList(pageNum,pageSize,stateId,applyDate.substring(0,10),userId);
            return Result.ok(pageInfo);
        }

    }
    @PutMapping
    public Result<?> withdrawn(@RequestParam Integer applyId,@RequestParam Integer applyNum,@RequestParam Integer articleId){
        if (applyService.update(applyId,applyNum,articleId)) {
            return Result.ok();
        }
        return Result.error("err");
    }
    @GetMapping("/getApply")
    public Result<?> getApply(@RequestParam Integer aId){
      Apply apply = applyService.getApply(aId);
      return Result.ok(apply);
    }
    @PutMapping("/rePlease")
    public Result<?> rePlease(@RequestParam Integer applyId,@RequestParam Integer applyNum,@RequestParam Integer articleId){
        if (applyService.updateSub(applyId,applyNum,articleId)) {
            return Result.ok();
        }
        return Result.error("err");
    }
    @PutMapping("/update")
    public Result<?> updateNum(Integer applyId,String applyReason,String applyDate,Integer applyNum) {
      if(applyService.updateApply(applyId,applyReason,applyDate.substring(0,10),applyNum)){
          return Result.ok();
      }

      return Result.error("err");
    }

}
